package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.ExtraUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 为数据添加一个会员，并为这个会员的密码加密
     * @param member
     */
    @Override
    public void add(Member member) {

        memberMapper.add(member);
    }

    /**
     * 获取过去12个月每月会员数量
     *
     * @return "data":{
     * "months":["2019.01","2019.02","2019.03","2019.04"],
     * "memberCounts":[3,4,8,10]
     * }
     */
    @Override
    public Map getMemberReport() {
        //获取日历对象取得过去十二个月
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);

        List<String> monthList = new ArrayList<>();
        List<Integer> memberNumList = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            String month = new SimpleDateFormat("yyyy-MM").format(time);

            String startDay = month + "-1";
            String endtDay = month + "-31";
            Integer memberCount = memberMapper.findMemberCountByDateInterval(startDay, endtDay);

            monthList.add(month);
            memberNumList.add(memberCount);

            calendar.add(Calendar.MONTH, +1);
        }

        Map<Object, Object> map = new HashMap<>(2);
        map.put("months", monthList);
        map.put("memberCounts", memberNumList);

        return map;
    }
    /**
     * 根据月份查询会员数量
     *
     * @param months
     * @return
     */
    @Override
    public Map getMemberReportByMonths(String[] months) {
        List<String> monthsList = DateUtils.getMonthsByDiffMonth(months);
        List<Integer> memberNumList = new ArrayList<>();

        for (String month : monthsList) {
            String startDay = month + "-1";
            String endtDay = month + "-31";
            Integer memberCount = memberMapper.findMemberCountByDateInterval(startDay, endtDay);
            memberNumList.add(memberCount);
        }

        Map<String, Object> map = new HashMap<>(2);
        map.put("months", monthsList);
        map.put("memberCounts", memberNumList);
        return map;
    }

    /**
     * 获取所有会员的性别年龄区分列表
     *
     * @return
     */
    @Override
    public Map getMemberByAgeReport() {
        List<Member> membersList = memberMapper.findAll();
        int mealData = 0;
        int femealData = 0;
        int mealData0 = 0;
        int mealData18 = 0;
        int mealData30 = 0;
        int mealData45 = 0;
        int femealData0 = 0;
        int femealData18 = 0;
        int femealData30 = 0;
        int femealData45 = 0;

        for (Member member : membersList) {
            int age = DateUtils.getAgeByDate(member.getBirthday(),new Date());
            if ("1".equals(member.getSex())) {
                mealData++;
                if (age > 0 && age <= 18) {
                    mealData0++;
                }else if (age > 18 && age <= 30) {
                    mealData18++;
                }else if (age > 30 && age <= 45) {
                    mealData30++;
                }else if (age > 45) {
                    mealData45++;
                }

            } else if ("2".equals(member.getSex())) {
                femealData++;
                if (age > 0 && age <= 18) {
                    femealData0++;
                }else if (age > 18 && age <= 30) {
                    femealData18++;
                }else if (age > 30 && age <= 45) {
                    femealData30++;
                }else if (age > 45) {
                    femealData45++;
                }
            }
        }
        List<Map> sexList = new ArrayList<>();
        sexList.add(ExtraUtils.KV2Map("男性",mealData));
        sexList.add(ExtraUtils.KV2Map("女性",femealData));

        List<Map> sexListByAge = new ArrayList<>();
        sexListByAge.add(ExtraUtils.KV2Map("男:0至18岁", mealData0));
        sexListByAge.add(ExtraUtils.KV2Map("男:18至30岁", mealData18));
        sexListByAge.add(ExtraUtils.KV2Map("男:30至45岁", mealData30));
        sexListByAge.add(ExtraUtils.KV2Map("男:45岁以上", mealData45));
        sexListByAge.add(ExtraUtils.KV2Map("女:0至18岁", femealData0));
        sexListByAge.add(ExtraUtils.KV2Map("女:18至30岁", femealData18));
        sexListByAge.add(ExtraUtils.KV2Map("女:30至45岁", femealData30));
        sexListByAge.add(ExtraUtils.KV2Map("女:45岁以上", femealData45));

        Map<String,Object> map = new HashMap(2);
        map.put("sexList", sexList);
        map.put("sexListByAge", sexListByAge);

        return map;
    }

}
